package com.tivo.jipviewer;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class ServiceRowTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 4278013396846049542L;
	
	public static final int SERVICE_ID = 0;
	public static final int SERVICE_CLASS = 1;
	public static final int SERVICE_IN_DATA = 2;
	public static final int SERVICE_OUT_DATA = 3;
	public static final int SERVICE_TOTAL_DATA = 4;
	
	public static final int SV_WIDTH[] = {50, 300, 50, 50, 50, };
	
	List<JipBundleService> bundleServiceRow = new ArrayList<JipBundleService>();
	
	public void add(List<JipBundleService> serviceList){

		for (JipBundleService service : serviceList){
			bundleServiceRow.add(service);
			int iLast = bundleServiceRow.size();
			fireTableRowsInserted(iLast, iLast);
		}
	}

	public void clear() {
        int iLast = bundleServiceRow.size();
        bundleServiceRow.clear();
        fireTableRowsDeleted(0, iLast);
    }


	@Override
	public int getColumnCount() {
		return 5;
	}
	
	 @SuppressWarnings("unchecked")
	public Class getColumnClass(int iColumn){
	    	return (iColumn == 1) ? String.class : Integer.class;
	    }

	@Override
	public int getRowCount() {
		return bundleServiceRow.size();
	}

	@Override
	public Object getValueAt(int rowIndx, int colIndx) {
		
		JipBundleService service = bundleServiceRow.get(rowIndx);
		switch(colIndx){
		case SERVICE_ID: return service.getID(); 
		case SERVICE_CLASS: return service.getClassListString(); 
		case SERVICE_IN_DATA: return service.getInData(); 
		case SERVICE_OUT_DATA: return service.getoutData();
		case SERVICE_TOTAL_DATA: return service.getTotalData();
		default:       throw new RuntimeException("bad column index");
		}
	}
	
	public String getColumnName(int iColumn) {
        switch (iColumn) {
        case SERVICE_ID : return JipBundleService.SERVICE_ID;
        case SERVICE_CLASS : return JipBundleService.SERVICE_CLASS;
        case SERVICE_IN_DATA : return JipBundleService.SERVICE_IN_DATA;
        case SERVICE_OUT_DATA : return JipBundleService.SERVICE_OUT_DATA;
        case SERVICE_TOTAL_DATA : return JipBundleService.SERVICE_TOTAL_DATA;
        default:       throw new RuntimeException("bad column index");
        }
	}
	
	private String[] splitLines(String columnText, int columnIndex){
		return new String[]{columnText};
	}
}
